<?php

/**
 * To change this template, choose Tools | Templates
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * and open the template in the editor.
 */

/**
 * Description of Condition
 *
 * @author ldpf
 */
class Condition implements iCRUD {
    /*
     * Attributes
     */

    private $_conditionID; //auto increment in the database
    private $_value;
    private $_conditionTypeID; //Foreign key to a pre set of conditions

    public function Condition($value, $conditionTypeID, $conditionID=-1) {
        $this->_conditionID = $conditionID;
        $this->_value = $value;
        $this->_conditionTypeID = $conditionTypeID;
    }

    /*
     * Accessors
     */

    public function get_conditionID() {
        return $this->_conditionID;
    }

    public function get_value() {
        return $this->_value;
    }

    public function get_conditionTypeID() {
        return $this->_conditionTypeID;
    }

    /*
     * Modifiers
     */

    public function set_conditionID($_conditionID) {
        $this->_conditionID = $_conditionID;
    }

    public function set_value($_value) {
        $this->_value = $_value;
    }

    public function set_conditionTypeID($_conditionTypeID) {
        $this->_conditionTypeID = $_conditionTypeID;
    }

    /**
     * create row in the database
     */
    public function create() {

        if ($this->_conditionID != -1)
            die("Unable to create, still existing !");
        else {
            Database::connect();
            $query = "INSERT INTO `ATOM` values (NULL,'" .
                    $this->_value . "','" .
                    $this->_conditionTypeID . "')";
            mysql_query($query) or die("ERROR NB " . mysql_errno() . " : " . mysql_error());
            $this->_idSujet = mysql_insert_id();
            Database::disconnect();
            return $this->_idSujet;
        }
    }

    /**
     * Read an object from the database
     * @return Condition
     */
    public static function read($key) {

        Database::connect();
        $query = "SELECT * FROM CONDITION WHERE condition_id = '" . $key . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        Database::disconnect();
        return new Condition($value, $condition_type_id, $condition_id);
    }

    /**
     * Update an object from the database
     */
    public function update() {


        Database::connect();
        $query = "UPDATE `CONDITION` SET value='" .
                $this->_value . "', condition_type_id='" .
                $this->_conditionTypeID . "' WHERE condition_id='" .
                $this->_conditionID . "'";
        mysql_query($query) or die("ERROR NB " . mysql_errno() . " : " . mysql_error());
        Database::disconnect();
    }

    /**
     * Delete an object from the database
     */
    public function delete() {

        Database::connect();
        $query = "DELETE FROM 'CONDITION` WHERE condition_id=" .
                $this->_conditionID . ";";
        mysql_query($query) or die(mysql_error());
        Database::disconnect();
    }

}
?>
